package com.sunnypower.rushfitapp;


import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.sunnypower.rushfitapp.entity.User;
import com.sunnypower.rushfitapp.util.GetData;


public class UserEditActivity extends AppCompatActivity implements View.OnClickListener {
    private Button backmain;
    private Button saveinfo;
    private User user;
    private ImageView img_user;
    private TextView tv_edit_username;
    private TextView tv_edit_location;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rushfit_useredit);
        init();
    }

    private void init() {
        Intent intent = getIntent();
        user = new User();
        user.setUid(Integer.valueOf(intent.getStringExtra("uid")));
        user.setName(intent.getStringExtra("name"));
        user.setSex(intent.getIntExtra("sex", 1));
        user.setAddr(intent.getStringExtra("addr"));
        user.setPhoto(intent.getStringExtra("photo"));
        user.setTel(intent.getStringExtra("username"));

        img_user = findViewById(R.id.img_user);
        tv_edit_username = findViewById(R.id.tv_edit_username);
        tv_edit_location = findViewById(R.id.tv_edit_location);

        tv_edit_username.setText(user.getName());
        tv_edit_location.setText(user.getAddr());

        byte[] decodedString = Base64.decode(user.getPhoto().split(",")[1], Base64.DEFAULT);
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        img_user.setImageBitmap(decodedByte);


        saveinfo = findViewById(R.id.saveinfo);
        backmain = findViewById(R.id.backmain);
        backmain.setOnClickListener(this);
        saveinfo.setOnClickListener(this);
        tv_edit_username.setOnClickListener(this);
        tv_edit_location.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.backmain:
                finish();
                break;
            case R.id.tv_edit_username:
                AlertDialog.Builder builder = new AlertDialog.Builder(UserEditActivity.this);
                LayoutInflater inflater = LayoutInflater.from(UserEditActivity.this);
                View viewDialog = inflater.inflate(R.layout.edit_name_alter, null);
                final EditText name = (EditText) viewDialog.findViewById(R.id.et_alter_name);
                name.setText(user.getName());
                builder.setView(viewDialog);
                builder.setTitle("编辑姓名");

                builder.setPositiveButton("保存", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (TextUtils.isEmpty(name.getText().toString().trim())) {
                            Toast toast = Toast.makeText(getApplicationContext(), "用户名称不能为空", Toast.LENGTH_SHORT);
                            toast.setGravity(Gravity.CENTER | Gravity.CENTER, 0, 0);
                            toast.show();
                        } else {
                            tv_edit_username.setText(name.getText());
                        }
                    }
                });
                builder.setNegativeButton("取消", null);
                builder.create().show();
                break;
            case R.id.tv_edit_location:
                intent = new Intent(UserEditActivity.this, UserLocActivity.class);
                intent.putExtra("addr", user.getAddr());
                startActivityForResult(intent, 0);
                break;
            case R.id.saveinfo:
                String uid = GetData.doGet("http://120.24.24.141:10990/rushfit/edituser?uid=" + user.getUid() + "&name=" + tv_edit_username.getText() + "&addr=" + tv_edit_location.getText());
                if (uid.equals(1)) {
                    Toast toast = Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER | Gravity.CENTER, 0, 0);
                    toast.show();
                }
                finish();
                intent = new Intent(UserEditActivity.this, UserMainActivity.class);
                intent.putExtra("uid", uid);
                intent.putExtra("username", user.getTel());
                startActivity(intent);
                break;
        }
    }

    //接收startActivityForResult的结果
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //返回模式
        int returnMode;
        //接收返回值
        returnMode = data.getExtras().getInt("mode", 0);

        if (returnMode == 0) {
            String addr = data.getExtras().getString("location");
            tv_edit_location.setText(addr);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
